using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FreeLancer.HRB.Domains;
using FreeLancer.HRB.Core;
using FreeLancer.HRB.Core.Web.Controls;

namespace FreeLancer.HRB.WebApp
{
  public partial class Category : WebBasePage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack && !IsCallback)
      //if (!IsCallback)
      {
        if (Request.RawUrl.Contains("?id=") && Request.QueryString["id"].Length == 36)
        {
          Guid id = new Guid(Request.QueryString["id"]);
          Categories categoryData = GlobalCache.CategoriesService.
            GetByCategoryIdLanguageId(id, _languageId);
          
          FreeLancer.HRB.Core.Category category = null;
          if(categoryData != null)
            category = new FreeLancer.HRB.Core.Category(categoryData);

          if (category != null)
          {
            Response.Clear();
            Response.StatusCode = 301;
            Response.AppendHeader("location", category.RelativeLink.ToString());
            Response.End();
          }
          else
          {
            Response.Redirect(Utils.RelativeWebRoot + "Error404.aspx", true);
          }
        }
      }

      if (Request.QueryString["id"] != null && Request.QueryString["id"].Length == 36)
      {
        InitializePageData();
      }
      else
      {
        Response.Redirect(Utils.RelativeWebRoot + "Error404.aspx", true);
      }
    }

    protected void btnView_Click(object sender, EventArgs e)
    {
      Guid id = new Guid(Request.QueryString["id"]);

      DataSourcePosts.SelectParameters.Clear();
      DataSourcePosts.SelectParameters.Add("CategoryId", id.ToString());
      DataSourcePosts.SelectParameters.Add("LanguageId", _languageId.ToString());
      DataSourcePosts.SelectParameters.Add("IsPublished", "True");
      DataSourcePosts.SelectParameters.Add("HeaderImageFolder", Utils.RelativeWebRoot + "Images/PostImages/");
      if (ASPxDateEdit.Value != null)
      {
        DataSourcePosts.SelectParameters.Add("DateCreated", ASPxDateEdit.Date.ToString("MM/dd/yyyy"));
      }
      else
      {
        Parameter param = new Parameter("DateCreated");
        param.Type = TypeCode.DBNull;
        DataSourcePosts.SelectParameters.Add(param);
      }
    }

    private void InitializePageData()
    {
      Guid id = new Guid(Request.QueryString["id"]);

      DataSourcePosts.SelectParameters.Clear();
      DataSourcePosts.SelectParameters.Add("CategoryId", id.ToString());
      DataSourcePosts.SelectParameters.Add("LanguageId", _languageId.ToString());
      DataSourcePosts.SelectParameters.Add("IsPublished", "True");
      DataSourcePosts.SelectParameters.Add("HeaderImageFolder", Utils.RelativeWebRoot + "Images/PostImages/");
      Parameter param = new Parameter("DateCreated");
      param.Type = TypeCode.DBNull;
      DataSourcePosts.SelectParameters.Add(param);
      
      Categories categoryData = GlobalCache.CategoriesService.
          GetByCategoryIdLanguageId(id, _languageId);

      anchorCategoryTitle.InnerText = categoryData.CategoryName;

      Title = categoryData.CategoryName + " - HRB";

      SpecialSettings(id);
    }

    private void SpecialSettings(Guid id)
    {
      //Needings
      if (id.ToString() == "792ad48a-3bec-4565-8600-5f9162e4b474")
      {
        anchorAddNews.HRef = Utils.RelativeWebRoot + "Needings.aspx";
        NewsBrowser.TextField = "PostContent";
      }
      else if (id.ToString() == "183bfcf8-abfe-40b2-9b7b-784d8fa84ea0") //Blog
      {
        anchorAddNews.HRef = Utils.RelativeWebRoot + "Blog.aspx";
        NewsBrowser.TextField = "PostContent";
        NewsBrowser.EncodeHtml = false;
      }
      else if (id.ToString() == "2e9e24b0-c419-48ec-91c0-1bdbb43a8500") //QnA
      {
        anchorAddNews.HRef = Utils.RelativeWebRoot + "QnA.aspx";
        NewsBrowser.TextField = "PostContent";
      }
      else
      {
        anchorAddNews.Visible = false;
        NewsBrowser.EncodeHtml = true;
        NewsBrowser.TextField = "Description";
      }
      
    }
  }
}
